/**
 * 邀请界面
 */
class invitationSub extends eui.Component{
    private return:eui.Button;
    private inviteNum:eui.Label;
    private score:eui.Label;
    private invite:eui.Button;
    private list:eui.List;
    private _arrayData:eui.ArrayCollection;

    private _score:number = 0;
    public constructor(){
        super();
       // this.cacheAsBitmap =true;
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "invitationSkin"; 
    }

    private createCompleteEvent():void{
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
        this.inIt();
    }

    private inIt(){
        this.return.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            this.dispatchEventWith("return");
        },this);
        Dispatcher.ins.addEventListener("awardList",this.setInviteList,this);
        Dispatcher.ins.addEventListener("awardExchange",this.successExchange,this);

        this.invite.addEventListener(egret.TouchEvent.TOUCH_TAP,()=>{
            Dispatcher.ins.dispatchEvent(new GameEvent("invite"));
        },this);

        this._arrayData =  new eui.ArrayCollection([]);
        this.list.dataProvider = this._arrayData;
        this.list.itemRenderer = invitationIR;
        this.list.addEventListener(eui.ItemTapEvent.ITEM_TAP,(evt:GameEvent)=>{
            var item = evt.target.selectedItem;
            if(item.Score > this._score){
                Dispatcher.ins.dispatchEvent(new GameEvent("toast","您的农场卡不够数量兑换该种子，请点击邀请码邀请好友吧！"));
            }else{
                let _model = new model("您有"+this._score+"张农场卡，兑换该种子需要"+item.Score+"张,是否进行兑换");
                _model.addEventListener("delete",()=>{
                    this.removeChild(_model);
                    let dataPost = {id:item.Id};
                    socket.request("awardExchange",dataPost);
                },this);
                this.addChild(_model);
            }
        },this);
        this.getInviteList();
    }

    public getInviteList():void{
        socket.request("awardList");
    }
    private setInviteList(evt:GameEvent):void{
        let obj = evt.data;
        let list = obj.list;
        this.inviteNum.text = obj.InviteNum;
        this.score.text = obj.score;
        this._score = obj.score;
        let arr = [];
        for(let i = 0,lg = list.length;i < lg;i ++){
            let trObj = list[i];
            trObj.title = trObj.AwardName + "X" + trObj.AwardNum;
            trObj.score = "农场卡-" + trObj.Score;
            trObj.icon = "harvest_" + trObj.AwardId + "_png";
            arr.push(trObj);
        }
        this._arrayData =  new eui.ArrayCollection(arr);
        this.list.dataProvider = this._arrayData;
        this._arrayData.refresh();
    }
    private successExchange(evt:GameEvent):void{
        let obj = evt.data;
        Dispatcher.ins.dispatchEvent(new GameEvent("toast","兑换成功"));
        this.score.text = obj.score;
        this._score = obj.score;
    }
}
class invitationIR extends eui.ItemRenderer{
    private exchange:eui.Image;
    public constructor(){
        super();
        this.addEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent,this);
        this.skinName = "invitationIRSkin";
    }
    private createCompleteEvent():void{
        super.createChildren();
        this.removeEventListener(eui.UIEvent.COMPLETE , this.createCompleteEvent, this);
    }
    protected dataChanged():void{

    }
}